class HomeController < ApplicationController
  skip_before_filter :verify_authenticity_token, :only => [:auto_complete_for_search_name, :change_category, :show_sideffects, :add_drug, :delete_drug, :switch_drugs_categories_list, :show_details, :show_category_sideffects, :auto_complete_for_add_drug, :sort_by_frequency, :sort_by_tolerance, :sort_by_sideffect, :show_age_chart, :show_sex_chart, :show_time_taken_chart, :sort_category_sideffects_by_name, :sort_category_sideffects_by_frequency, :sort_drugs_table_by_name, :sort_drugs_table_by_score, :sort_drugs_table_by_ratings, :sort_drugs_table_by_sideffects]
  def index
    reset_session
    @is_drug = true
    @top_sideffects = Sideffect.get_top_sideffects
    @top_drugs = Drug.get_top_drugs
    @total_drugs = Drug.count
    @total_ratings = Rating.count
  end
  def sort_by_tolerance
    drug = Drug.find(params[:id])
    sideffect = {:drug => drug, :sideffects => drug.get_sideffects_by_tolerance(session[:sex] || "All", session["age"] || "All", session[:time_taken] || "All"), :total => drug.get_rating_count(session[:sex] || "All", session[:age] || "All", session[:time_taken] || "All")}
    render(:partial => "show_drug_sideffects", :locals => {:sideffect => sideffect})
  end
  def sort_by_frequency
    drug = Drug.find(params[:id])
    sideffect = {:drug => drug, :sideffects => drug.get_sideffects_by_frequency(session[:sex] || "All", session[:age] || "All", session[:time_taken] || "All"), :total => drug.get_rating_count(session[:sex] || "All", session[:age] || "All", session[:time_taken] || "All")}
    render(:partial => "show_drug_sideffects", :locals => {:sideffect => sideffect})
  end
  def sort_by_sideffect
    drug = Drug.find(params[:id])
    sideffect = {:drug => drug, :sideffects => drug.get_sideffects_by_sideffect(session[:sex] || "All", session[:age] || "All", session[:time_taken] || "All"), :total => drug.get_rating_count(session[:sex] || "All", session[:age] || "All", session[:time_taken] || "All")}
    render(:partial => "show_drug_sideffects", :locals => {:sideffect => sideffect})
  end
  def list_drugs
    reset_session
    @is_drug = true
    @initial = params[:id]
    @drugs = Drug.find_by_initial(@initial)
  end

  def list_categories
    reset_session
    @is_drug = false
    @initial = params[:id]
    @categories = Category.find_by_initial(@initial)
  end

  def auto_complete_for_search_name
    @drug_names = Drug.find_by_initial(params[:search][:name])
    @category_names = Category.find_by_initial(params[:search][:name])
    render(:partial => "auto_complete_name", :layout => false) 
  end

  def auto_complete_for_add_drug
    @drug_names = Drug.find_by_initial(params[:add][:drug])
    @category_names = []
    render(:partial => "auto_complete_name", :layout => false)
  end

  def show_category
    @is_drug = false
    @category = Category.find(params[:id])
    @drugs = @category.drugs
    session[:drugs_c] = @drugs.collect {|d| d.id}
    session[:sex_c] = "All"
    session[:age_c] = "All"
    session[:time_taken_c] = "All"
    session[:category] = params[:id] 
    @sideffects = Category.get_sideffects(session[:drugs_c], "All", "All", "All")
    @total = @category.get_rating_number(session[:drugs_c], session[:sex_c], session[:age_c], session[:time_taken_c])
  end

  def show_details
    @sideffect = Sideffect.find(params[:id])
    #@sideffect_total = @sideffect.get_category_total(session[:category], session[:drugs_c], session[:sex_c] || "All", session[:age_c] || "All", session[:time_taken_c] || "All")
    @detail_sideffects = Category.get_details(params[:id], session[:drugs_c], session[:sex_c] || "All", session[:age_c] || "All", session[:time_taken_c] || "All")
    render(:partial => "show_details", :layout => false)
  end

  def sort_details_by_name
    @sideffect = Sideffect.find(params[:id])
    @detail_sideffects = Category.get_details(params[:id], session[:drugs_c], session[:sex_c] || "All", session[:age_c] || "All", session[:time_taken_c] || "All")
    render(:partial => "show_details", :layout => false)
  end

  def sort_details_by_frequency
    @sideffect = Sideffect.find(params[:id])
    @detail_sideffects = Category.get_details(params[:id], session[:drugs_c], session[:sex_c] || "All", session[:age_c] || "All", session[:time_taken_c] || "All", "frequency")
    render(:partial => "show_details", :layout => false)
  end

  def sort_details_by_ratings
    @sideffect = Sideffect.find(params[:id])
    @detail_sideffects = Category.get_details(params[:id], session[:drugs_c], session[:sex_c] || "All", session[:age_c] || "All", session[:time_taken_c] || "All", "ratings")
    render(:partial => "show_details", :layout => false)
  end

  def sort_category_sideffects_by_name
    @total = Category.find(session[:category]).get_rating_number(session[:drugs_c], session[:sex_c], session[:age_c], session[:time_taken_c])
    @sideffects = Category.get_sideffects(session[:drugs_c], session[:sex_c], session[:age_c], session[:time_taken_c], "name")
    render(:partial => "show_category_sideffects", :layout => false)
  end
 
  def sort_category_sideffects_by_frequency
    @total = Category.find(session[:category]).get_rating_number(session[:drugs_c], session[:sex_c], session[:age_c], session[:time_taken_c])
    @sideffects = Category.get_sideffects(session[:drugs_c], session[:sex_c], session[:age_c], session[:time_taken_c])
    render(:partial => "show_category_sideffects", :layout => false)
  end

  def show_category_sideffects 
    session[:drugs_c] = params[:drugs] unless params[:drugs].blank?
    session[:sex_c] = params[:sex] unless params[:sex].blank?
    session[:age_c] = params[:age] unless params[:age].blank?
    session[:time_taken_c] = params[:time_taken] unless params[:time_taken].blank?
    @total = Category.find(session[:category]).get_rating_number(session[:drugs_c], session[:sex_c], session[:age_c], session[:time_taken_c])
    @sideffects = Category.get_sideffects(session[:drugs_c], session[:sex_c], session[:age_c], session[:time_taken_c])
    render(:partial => "show_category_sideffects", :layout => false)
  end

  def show_drug
    @is_drug = true
    @more_drugs = Drug.find(:all)
    @more_drugs.delete_if {|d| d.id == params[:id]}
    drug = Drug.find(params[:id])
    categories = drug.categories
    @sideffects_list = [{:drug => drug, :categories => categories, :current_category_id => categories[0].id, :rating => drug.get_rating, :rating_rank => drug.get_rating_rank(categories[0].id), :popularity => drug.get_popularity(categories[0].id),:sideffects => drug.get_sideffects("All", "All", "All"), :total => drug.get_rating_count("All", "All", "All")}]
    session[:drugs] = [drug.id.to_s]
    session[:sex] = "All"
    session[:age] = "All"
    session[:time_taken] = "All"
  end

  def show_drug2
    @is_drug = true
    @drug = Drug.find(params[:id])
  end

  def show_age_chart
    drug = Drug.find(params[:id])
    chart = Ziya::Charts::Bar.new
    chart.add(:axis_category_text, ["<18", "18-45", "45-60", ">60", "Unknown"])
    chart.add(:series, "Count", [drug.get_stat("All", "18", "All"), drug.get_stat("All", "45", "All"), drug.get_stat("All", "60", "All"), drug.get_stat("All", "inf", "All"), drug.get_stat("All", "U", "All")])
    respond_to do |fmt|
      fmt.xml {render :xml => chart.to_xml}
    end
  end

  def show_sex_chart
    drug = Drug.find(params[:id])
    chart = Ziya::Charts::Bar.new
    chart.add(:axis_category_text, ["Female", "Male", "Unknown"])
    chart.add(:series, "Count", [drug.get_stat("F", "All", "All"), drug.get_stat("M", "All", "All"), drug.get_stat("U", "All", "All")])
    respond_to do |fmt|
      fmt.xml {render :xml => chart.to_xml}
    end
  end

  def show_time_taken_chart
    drug = Drug.find(params[:id])
    chart = Ziya::Charts::Bar.new
    chart.add(:axis_category_text, ["1 week", "1 month", "3 months", "6 months", "1 year", ">1 year", "Unknown"])
    chart.add(:series, "Count", [drug.get_stat("All", "All", "week"), drug.get_stat("All", "All", "month"), drug.get_stat("All", "All", "season"), drug.get_stat("All", "All", "half_year"), drug.get_stat("All", "All", "year"), drug.get_stat("All", "All", "inf"), drug.get_stat("All", "All", "U")])
    respond_to do |fmt|
      fmt.xml {render :xml => chart.to_xml}
    end
  end

  def show_sideffect_chart
    drug = Drug.find(params[:id])
    sideffects = drug.get_sideffects(session[:sex] || "All", session[:age] || "All", session[:time_taken] || "All")
    chart = Ziya::Charts::Pie.new
    category_text = sideffects[0..4].collect{|sf| sf["name"][0..10]}
    series = sideffects[0..4].collect do |sf| 
      sf["cnt"].to_i
      #Hash.new(:label => sf["name"], :value => sf["cnt"].to_i)
    end
    unless sideffects[5].blank?
      category_text << "others"
      series <<  sideffects[5..-1].inject(0){|sum, e| sum + e["cnt"].to_i}
    end
    chart.add(:axis_category_text, category_text)
    chart.add(:series, "Frequency", series)
    respond_to do |fmt|
      fmt.xml {render :xml => chart.to_xml}
    end
  end

  def change_category
    drug = Drug.find(params[:id])
    categories = drug.categories
    sideffect = {:drug => drug, :categories => categories, :current_category_id => params[:category].to_i, :rating => drug.get_rating, :rating_rank => drug.get_rating_rank(params[:category].to_i), :popularity => drug.get_popularity(params[:category].to_i), :total => drug.ratings.count}
    render(:partial => "show_drug_header", :locals => {:sideffect => sideffect}, :layout => false)
  end

  def show_sideffects
    @sideffects_list = []
    build_sideffects_list_from_session(params[:sex], params[:age], params[:time_taken]) if session[:drugs]
    session[:sex] = params[:sex]
    session[:age] = params[:age]
    session[:time_taken] = params[:time_taken]
    render(:partial => "show_sideffects", :layout => false)
  end

  def add_drug
    drug = Drug.find(:first, :conditions => ["name = ?", params[:add][:drug]])
    categories = drug.categories
    @sideffects_list = []
    build_sideffects_list_from_session(session[:sex] || "All", session[:age] || "All", session[:time_taken] || "All") unless session[:drugs].blank?
    @sideffects_list << {:drug => drug, :categories => categories, :current_category_id => categories[0].id, :rating => drug.get_rating, :rating_rank => drug.get_rating_rank(categories[0].id), :popularity => drug.get_popularity(categories[0].id),:sideffects => drug.get_sideffects(session[:sex] || "All", session[:age] || "All", session[:time_taken] || "All"), :total => drug.get_rating_count(session[:sex] || "All", session[:age] || "All", session[:time_taken] || "All")}
    if session[:drugs]
      session[:drugs] << drug.id.to_s 
    else 
      session[:drugs] = [more_drug.id.to_s]
    end
    render(:partial => "show_sideffects", :layout => false)
  end

  def delete_drug
    session[:drugs].delete_if {|d| d == params[:id]} if session[:drugs]
    @sideffects_list = []
    build_sideffects_list_from_session(session[:sex] || "All", session[:age] || "All", session[:time_taken] || "All")
    render(:partial => "show_sideffects", :layout => false)
  end

  def switch_drugs_categories_list
    @is_drug = params[:is_drug] == "drugs" ? true : false
    render(:partial => "list_letters", :layout => false)
  end

  def search
    drug = Drug.find(:first, :conditions => ["name = ?", params[:search][:name]])
    category = Category.find(:first, :conditions => ["name = ?", params[:search][:name]]) if drug.blank?
    redirect_to :action => "show_drug", :id => drug.id unless drug.blank?
    redirect_to :action => "show_category", :id => category.id unless category.blank?
    redirect_to :action => "index" if drug.blank? and category.blank?
  end

  def sort_drugs_table_by_name
    @category = Category.find(params[:id])
    @drugs = @category.drugs.sort {|x, y| x.name <=> y.name}
    session[:drugs_c] = @drugs.collect {|d| d.id}
    render(:partial => "show_drugs_table", :layout => false)
  end
  
  def sort_drugs_table_by_score
    @category = Category.find(params[:id])
    @drugs = @category.drugs.sort {|x, y| y.get_rating <=> x.get_rating}
    session[:drugs_c] = @drugs.collect {|d| d.id}
    render(:partial => "show_drugs_table", :layout => false)
  end

  def sort_drugs_table_by_ratings
    @category = Category.find(params[:id])
    @drugs = @category.drugs.sort {|x, y| y.get_rating_count("All", "All", "All") <=> x.get_rating_count("All", "All", "All")}
    session[:drugs_c] = @drugs.collect {|d| d.id}
    render(:partial => "show_drugs_table", :layout => false)
  end
  def sort_drugs_table_by_sideffects
    @category = Category.find(params[:id])
    @drugs = @category.drugs.sort {|x, y| y.get_sideffect_count <=> x.get_sideffect_count}
    session[:drugs_c] = @drugs.collect {|d| d.id}
    render(:partial => "show_drugs_table", :layout => false)
  end
private
  def build_sideffects_list_from_session(sex, age, time_taken)
    session[:drugs].each do |d|
      drug = Drug.find(d)
      categories = drug.categories
      @sideffects_list << {:drug => drug, :categories => categories, :current_category_id => categories[0].id, :rating => drug.get_rating, :rating_rank => drug.get_rating_rank(categories[0].id), :popularity => drug.get_popularity(categories[0].id), :sideffects => drug.get_sideffects(sex, age, time_taken), :total => drug.get_rating_count(sex, age, time_taken)}
    end
  end
end
